/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package system;

import java.util.LinkedList;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import tools.dbconnector;

/**
 *
 * @author Muhammad Anis
 */
public class category {
    //memiliki beberapa sub kategory
    List<subcategory> subcategory  = new LinkedList<subcategory>();
    //memiliki room
    List<room> room = new LinkedList<room>();
    //memiliki nama
    String nama;
    //memiliki moderator
    String moderator;
    //memiliki status collapse
    String visibility = "hidden";
    
    public category(String nama,String moderator){
        this.nama = nama;
        this.moderator = moderator;
    }

    public void insertSubCategory(subcategory sub){
        subcategory.add(sub);
    }

    public void insertSubCategory(List<subcategory> input){
        if(input!=null)
            subcategory.addAll(input);
    }
    //fungsi-fungsi yang dimiliki
    //meload semua subkategori
    public void load(){
        dbconnector db = new dbconnector();
        insertSubCategory(db.getAllSubDirectory(nama));
    }

    //memprint semua room dan subcategory
    public void print(JspWriter out){
        for(room r : room){
            r.print(out);
        }
        for(subcategory s : subcategory){
            s.print(out);
        }
    }
	
    public void update(){
        //tiap subkategori akan mengupdate isinya
        dbconnector db = new dbconnector();
        List<subcategory> temp = db.getAllSubDirectory(this.nama);
        for(subcategory s:temp){
            if(!isHasSubCategory(s))
                subcategory.add(s);
        }
    }

    public boolean isHasSubCategory(subcategory s){
        for(subcategory su : subcategory){
            if(su.nama.equals(s.nama))
                return true;
        }
        return false;
    }

    public subcategory getSubCategory(String name){
        for(subcategory s : subcategory){
            if(s.nama.equals(name)){
                return s;
            }
        }
        return null;
    }

    public void setVisibility(String status){
        this.visibility = status;
        for(subcategory s:subcategory){
            s.setVisibility(status);
        }
    }

    public boolean addUserInRoom(String roomname,String username){
        for(room r : room){
            if(r.nama.equals(roomname)){
                r.addUser(username);
                return true;
            }
        }
        for(subcategory s:subcategory){
            return s.addUserinRoom(roomname, username);
        }
        return false;
    }

    public boolean addChatinRoom(String username,String roomaName,String message){
        for(room r : room){
            if(r.nama.equals(roomaName)){
                r.addChat(username, message);
                return true;
            }
        }
        for(subcategory s:subcategory){
            return s.addChatinRoom(username, roomaName, message);
        }
        return false;
    }

    public room getRoom(String roomname){
        for(room r : room){
            if(r.nama.equals(roomname))
                return r;
        }
        for(subcategory s :subcategory){
            room r = s.getRoom(roomname);
            if(r!=null)
                return r;
        }
        return null;
    }

    public List<String> getAllSubcategoryName(){
        List<String> temp = new  LinkedList<String>();
        for(subcategory s : subcategory){
            temp.add(s.nama);
        }
        return temp;
    }
    
    public List<room> getAllRoom(){
        List<room> temp;
        temp = room;
        for(subcategory s : subcategory){
            temp.addAll(s.getAllRoom());
        }
        return temp;
    }
}
